/// Business logic class defined by OpenAPI in HCC.Interface.PubSub.spec<br/>
/// Updated May 11, 2023 13:27:10
Class HCC.Interface.PubSub.impl Extends %REST.Impl [ ProcedureBlock ]
{

/// If ExposeServerExceptions is true, then details of internal errors will be exposed.
Parameter ExposeServerExceptions = 1;

/// 新增主题<br/>
/// The method arguments hold values for:<br/>
///     body, newtopic<br/>
ClassMethod newtopic(body As %Stream.Object) As %DynamicObject
{
    #dim response as %Library.DynamicObject = ##class(%Library.DynamicObject).%New()
    Try {
        
    Set asc=##class(Ens.Util.JSON).JSONStreamToObject(body,.topic,"HCC.SVR.PubSub.Topic",1)
    If $$$ISERR(asc) {
        
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("请提供正确的 JSON",asc,400)
    }
    if (##class(HCC.SVR.PubSub.Topic).IdxTopicNameExists(topic.TopicName)) {
        set tTopic=##class(HCC.SVR.PubSub.Topic).IdxTopicNameOpen(topic.TopicName)
        set tTopic.SchemaType=topic.SchemaType
        Set tsc= tTopic.%Save()
        If $$$ISERR(tsc) {
            
            Do ..%SetStatusCode(500)
            Set response.TopicName=topic.TopicName
            Set response.Message = "主题更新失败"
            return response
        }
        Do ..%SetStatusCode(200)
        Set response.TopicName=topic.TopicName
        Set response.Message = "主题已更新"
        Return response
    }
    Set tsc=topic.%Save()
    If $$$ISERR(tsc) {
       
        Do ..%SetStatusCode(409)
       Set response.TopicName=topic.TopicName
       Set response.Message = "主题保存失败"
       Return response
    }
    Do ..%SetStatusCode(201)
    Set response.TopicName=topic.TopicName
    Set response.Message = "主题已添加"
    }
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("添加新主题错误",ex.AsStatus(),500)
    }
    Return response
}

/// get topic<br/>
/// The method arguments hold values for:<br/>
///     Topic, get topic<br/>
ClassMethod gettopic(Topic As %String) As %DynamicObject
{
   
    #dim topic as HCC.SVR.PubSub.Topic
    #dim response as %Library.DynamicObject
    Try {
          Set dynamicArray =##class(%Library.DynamicArray).%New()
    If ($GET(Topic)="") {
       Set myquery="select ID from HCC_SVR_PubSub.Topic" 
    Set tStatement = ##class(%SQL.Statement).%New()
    Set qStatus = tStatement.%Prepare(myquery)
    If qStatus'=1 {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("查询错误",qStatus,500)
    }
    Set rset = tStatement.%Execute()
    While rset.%Next() {
      Set ID=rset.%Get("ID")
      Set topic= ##class(HCC.SVR.PubSub.Topic).%OpenId(ID)
      Set dynamicObject =##class(%Library.DynamicObject).%New()
      Do dynamicObject.%Set("ID",ID)
      Do dynamicObject.%Set("TopicName",topic.TopicName)
      Do dynamicObject.%Set("SchemaType",topic.SchemaType)
	  Do dynamicArray.%Push(dynamicObject)
      
    }
    }
    Else{
     
     Set topic=##class(HCC.SVR.PubSub.Topic).IdxTopicNameOpen(Topic)
     Set dynamicObject =##class(%Library.DynamicObject).%New()
      Do dynamicObject.%Set("ID",topic.%Id())
      Do dynamicObject.%Set("TopicName",topic.TopicName)
      Do dynamicObject.%Set("SchemaType",topic.SchemaType)
	  Do dynamicArray.%Push(dynamicObject)
      }
    Do ..%SetStatusCode(200)
     Set response = dynamicArray
    }
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("查询错误",ex.AsStatus(),500)
    }
   
    Return response
}

/// 按照id删除主题<br/>
/// The method arguments hold values for:<br/>
///     topicname, removetopic<br/>
ClassMethod removetopic(topicname As %String) As %DynamicObject
{
    #dim response as %Library.DynamicObject = ##class(%Library.DynamicObject).%New()
    try{
    Set tsc= ##class(HCC.SVR.PubSub.Topic).IdxTopicNameDelete(topicname)
    If $$$ISERR(tsc) {
        
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("主题不存在",tsc,404)
    }

    Do ..%SetStatusCode(200)
    Set response.TopicName=topicname
    Set response.Message = "主题已删除"}
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("删除主题失败",ex.AsStatus(),500)
    }
    Return response
}

/// 新增订阅者<br/>
/// The method arguments hold values for:<br/>
///     body, newsubscriber<br/>
ClassMethod newsubscriber(body As %Stream.Object) As %DynamicObject
{
    #dim response as %Library.DynamicObject = ##class(%Library.DynamicObject).%New()
    #dim subscriber as HCC.SVR.PubSub.Subscriber
    #dim tsub as %Library.DynamicObject
    // json 转化为对象 
    try{
    Set bConfigid=""
    Set asc=##class(Ens.Util.JSON).JSONStreamToObject(body,.subscriber,"HCC.SVR.PubSub.Subscriber",1)
    Set tsub=##class(%Library.DynamicObject).%FromJSON(body)
      Set bsc = ..ValidateJSON(tsub,.jsonerror)
    If $$$ISERR(bsc) {
        return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("请提供正确的 JSON",bsc,400)
    }
    Set response.SubscriberName= tsub.SubscriberName
    Set IsNewsub= 1
    Set origenalCredential= ""
    If $$$ISERR(asc) {
        return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("请提供正确的 JSON",asc,400)
    }
    //检查使用的credential是否存在
        If (tsub.TargetAuthConfigName'="")&&(##class(Ens.Config.Credentials).%OpenId(tsub.TargetAuthConfigName) = "") {
        Do ..%SetStatusCode(400)
        Set response.TargetAuthConfigName=tsub.TargetAuthConfigName
        Set response.Message = "使用的Credential不存在,请与管理员确认"
        Return response
        }
    //增加target BO
    Set bName = subscriber.Target
    Set cID=""
    // 根据host名字，查看要添加的BO是否已经存在。如果不存在，则添加。如果存在，则更新
    &sql(select %ID into :cID from Ens_Config.Item where Name = :bName)
    
    #Dim production as Ens.Config.Production = ##class(Ens.Config.Production).%OpenId(tsub.TargetProduction)
    if production = "" {
        Do ..%SetStatusCode(400)
        Set response.TargetProduction=tsub.TargetProduction
        Set response.Message = "使用的Production不存在,请与管理员确认"
        Return response
    }
  If ( cID ="")
  {	     
      //如果BO不存在，在Production中添加新的BO
      #Dim BusinessHost as Ens.Config.Item = ##class(Ens.Config.Item).%New()
      #Dim Setting as Ens.Config.Setting 
      #dim SettingCre as Ens.Config.Setting  
      #dim SettingSA as Ens.Config.Setting 
      Set bConfigid=BusinessHost.%Id()
      // 设置BO的Production 和 settings
      if tsub.TargetURL'=""{
      set Setting=##class(Ens.Config.Setting).%New()
      Set Setting.Name="WebServiceURL"
      Set Setting.Value=tsub.TargetURL
      Do BusinessHost.Settings.Insert(Setting)
      }
      if tsub.TargetAuthConfigName'=""{
      set SettingCre=##class(Ens.Config.Setting).%New()
      // 配置soap credential
      Set SettingCre.Name="SOAPCredentials"
      Set SettingCre.Value=tsub.TargetAuthConfigName
      Do BusinessHost.Settings.Insert(SettingCre)
      }
      // 配置soap action
      if tsub.TargetSoapAction'=""{
        set SettingSA=##class(Ens.Config.Setting).%New()
      Set SettingSA.Name="SoapAction"
      Set SettingSA.Target="Host"
      Set SettingSA.Value=tsub.TargetSoapAction
      Do BusinessHost.Settings.Insert(SettingSA)
      }
      if tsub.TargetClass'=""{
        set tsub.TargetClass="HCC.SVR.Prod.BO.StandardHCCOutBound"
      }
        //设置BO的类名
      if (##class(%Dictionary.CompiledClass).%ExistsId(tsub.TargetClass))
      {
      Set BusinessHost.ClassName=tsub.TargetClass}
      Else
      {
        return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("提供的的BO类名不存在",bsc,400)
      }

      Set BusinessHost.Name=tsub.Target
      Set BusinessHost.Production = production
      //将BO插入Production，并保存Production
      Do production.Items.Insert(BusinessHost)
      Set tsc = production.%Save()
      
      //如果保存新建bo时，有任何错误，则返回失败信息
      If $$$ISERR(tsc) {
        return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("保存BO失败",tsc,500)
      }
      //更新Production
      Do ##class(Ens.Director).UpdateProduction()
  }
  Else
  {
    //如果相同名字的BO已经存在，则根据BO的ID更新其WebServiceURL、SOAPCredentials 和 SoapAction
    Set cItem= ##class(Ens.Config.Item).%OpenId(cID)
    For index=1:1:cItem.Settings.Count()
    {
      If (cItem.Settings.GetAt(index).Name ="WebServiceURL")
        {
          Set newS =##class(Ens.Config.Setting).%New()
          Set newS.Name="WebServiceURL"
          Set newS.Value=tsub.TargetURL
          Do cItem.Settings.SetAt(newS,index)
        }
        If (cItem.Settings.GetAt(index).Name ="SOAPCredentials")  
        {
          Set newS =##class(Ens.Config.Setting).%New()
          Set newS.Name="SOAPCredentials"
          Set newS.Value=tsub.TargetAuthConfigName
          Do cItem.Settings.SetAt(newS,index)
          }  
        If (cItem.Settings.GetAt(index).Name ="SoapAction")  
        {
          Set newS =##class(Ens.Config.Setting).%New()
          Set newS.Name="SoapAction"
          Set newS.Target="Host"
          Set newS.Value=tsub.TargetSoapAction
          Do cItem.Settings.SetAt(newS,index)
        }  
    }
    Set tsc= cItem.%Save() 
    If $$$ISERR(tsc) {
        return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("更新BO失败",tsc,500)
      }
      //更新Production
    Do ##class(Ens.Director).UpdateProduction() 	  
  }
   //插入或者更新订阅者
       If (##class(HCC.SVR.PubSub.Subscriber).IdxSubscriberNameExists(tsub.SubscriberName)) { 
            // 订阅者已存在 更新记录
            Set IsNewsub= 0
            &sql(
            update HCC_SVR_PubSub.Subscriber set 
            Target=:tsub.Target,
            IsActive=:tsub.IsActive,
            Priority=:tsub.Priority,
            PersonalContactName=:tsub.PersonalContactName,
            PersonalContact=:tsub.PersonalContact,
            "Role"=:tsub.Role 
            where SubscriberName=:tsub.SubscriberName
            )
            If SQLCODE=0 {
             set response.ID=##class(HCC.SVR.PubSub.Subscriber).IdxSubscriberNameOpen(tsub.SubscriberName).%Id()  
             Set response.Message="订阅者已更新"
             Do ..%SetStatusCode(200)
             Return response
             }
            Else {
            return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("更新订阅者失败",$$$OK,500)
            }
        }
        Else{
            //如果不存在 插入新纪录
            Set tsc=subscriber.%Save()
            If $$$ISERR(tsc) {
                Set tsc =..RollBackConfigItem(production,bConfigid)
                Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("保存订阅者失败",tsc,500)
            }
            Else {
                set response.ID=##class(HCC.SVR.PubSub.Subscriber).IdxSubscriberNameOpen(tsub.SubscriberName).%Id()
                Set response.Message = "新订阅者保存成功"
                Do ..%SetStatusCode(201)
            }
            }

    
    } Catch ex {
        return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("新增订阅者失败",ex.AsStatus(),500)
    }
    Return response
}

/// 获取订阅者<br/>
/// The method arguments hold values for:<br/>
///     ID, get subscriber using ID<br/>
///     SubscriberName, get subscriber using name<br/>
ClassMethod getsubscriber(ID As %String, SubscriberName As %String) As %DynamicObject
{
    #dim subscriber as HCC.SVR.PubSub.Subscriber
    #dim response as %Library.DynamicObject=##class(%Library.DynamicObject).%New()
    Try {
        Set dynamicArray =##class(%Library.DynamicArray).%New()
    If ($GET(ID)="")&($GET(SubscriberName)="") {
           Set myquery="select ID from HCC_SVR_PubSub.Subscriber" 
    Set tStatement = ##class(%SQL.Statement).%New()
    Set qStatus = tStatement.%Prepare(myquery)
    If qStatus'=1 {Write "%Prepare failed:" Do $SYSTEM.Status.DisplayError(qStatus) Quit}
    Set rset = tStatement.%Execute()
    While rset.%Next() {
      Set ID=rset.%Get("ID")
      
      Set subscriber= ##class(HCC.SVR.PubSub.Subscriber).%OpenId(ID)
      Do subscriber.%JSONExportToStream(.stream)
      
      Set dynamicObject =##class(%Library.DynamicObject).%FromJSON(stream)
      Set dynamicObject.ID=ID
      
	  Do dynamicArray.%Push(dynamicObject)
    
    }
    }ElseIf ($GET(ID)'="")&($GET(SubscriberName)=""){
        Set subscriber= ##class(HCC.SVR.PubSub.Subscriber).%OpenId(ID)
        Do subscriber.%JSONExportToStream(.stream)
        Set dynamicObject =##class(%Library.DynamicObject).%FromJSON(stream)
        Set dynamicObject.ID=ID
        Do dynamicArray.%Push(dynamicObject)
    }ElseIf ($GET(ID)="")&($GET(SubscriberName)'=""){
         Set subscriber= ##class(HCC.SVR.PubSub.Subscriber).IdxSubscriberNameOpen(SubscriberName)
        Do subscriber.%JSONExportToStream(.stream)
        Set dynamicObject =##class(%Library.DynamicObject).%FromJSON(stream)
        Set dynamicObject.ID=subscriber.%Id()
        Do dynamicArray.%Push(dynamicObject)
        }Else{
            Do ..%SetStatusCode(400)
            Set response.Message="ID 和 SubscriberName 不能同时使用"
            Return response
        }
    Do ..%SetStatusCode(200)
     Set response= dynamicArray
    }
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("获取订阅者错误",ex.AsStatus(),500)
    }
    Return response
}

/// 按照id删除订阅者<br/>
/// The method arguments hold values for:<br/>
///     id, removesubscriber<br/>
ClassMethod removesubscriber(id As %Integer) As %DynamicObject
{
 #Dim sub as HCC.SVR.PubSub.Subscriber
 #dim configItem as Ens.Config.Item
 #dim response as %Library.DynamicObject=##class(%Library.DynamicObject).%New()
 Try{
 Set sub = ##class(HCC.SVR.PubSub.Subscriber).%OpenId(id)
 Set tconId=""
 If sub="" {
     
     Return response
 }
 Set configItemName=sub.Target
 Set response.SubscriberName=sub.SubscriberName
   Set tsc = ##class(HCC.SVR.PubSub.Subscriber).%DeleteId(id)
    If $$$ISERR(tsc) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("订阅者删除失败",tsc.AsStatus(),500)
    }
    Else {
        Set response.Message="订阅者删除成功"
    }
  #Dim production as Ens.Config.Production = ##class(Ens.Config.Production).%OpenId("HCC.SVR.Prod.FoundationProduction")
  Set configItemName=sub.Target
  Set myquery= "select ID from Ens_Config.Item where Name= ? and ClassName='HCC.SVR.Prod.BO.StandardHCCOutBound'"
  Set tStatement = ##class(%SQL.Statement).%New()
    Set qStatus = tStatement.%Prepare(myquery)
    If qStatus'=1 {Write "%Prepare failed:" Do $SYSTEM.Status.DisplayError(qStatus) Quit}
    Set rset = tStatement.%Execute(configItemName)
    While rset.%Next() {
      Set tconId= rset.%Get("ID")    
    }
    If tconId=""
    
    {
       Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("订阅者已删除,但未找到订阅者对应的组件",$$$OK,500)
	    }
  Set ConItem=##class(Ens.Config.Item).%OpenId(tconId)
  Set tsc= ..RollBackConfigItem(production,tconId)
  Do ##class(Ens.Config.Item).%DeleteId(tconId)
  Do ##class(Ens.Director).UpdateProduction()   
  Do ..%SetStatusCode(200)
 }
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("删除订阅者错误",ex.AsStatus(),500)
    }
 Return response
}

/// 新增订阅<br/>
/// The method arguments hold values for:<br/>
///     body, newsubscription<br/>
ClassMethod newsubscription(body As %Stream.Object) As %DynamicObject
{
    #dim response as %Library.DynamicObject = ##class(%Library.DynamicObject).%New()
    #dim subscription as HCC.SVR.PubSub.Subscription =##class(HCC.SVR.PubSub.Subscription).%New()
    Try{
    Set tsubscription=##class(%Library.DynamicObject).%FromJSON(body)
    Set tsc=..ValidateSubscriptionJSON(tsubscription,.errors)
    If $$$ISERR(tsc) {
        
        Do ..%SetStatusCode(400)   
        Set response=errors
        Return response
    }
    //判断订户是否存在
    Set subscriberExist= ##class(HCC.SVR.PubSub.Subscriber).IdxSubscriberNameExists(tsubscription.SubscriberName)
    If 'subscriberExist{
       
       Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("订阅者在系统中不存在",$$$OK,500)
    }
     Set topicExist= ##class(HCC.SVR.PubSub.Topic).IdxTopicNameExists(tsubscription.TopicName)
    If 'topicExist{
       Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("订阅的主题在系统中不存在",$$$OK,500)
    }
    Set subscriber= ##class(HCC.SVR.PubSub.Subscriber).IdxSubscriberNameOpen(tsubscription.SubscriberName)
    Set topic= ##class(HCC.SVR.PubSub.Topic).IdxTopicNameOpen(tsubscription.TopicName)
     Set response.Name=tsubscription.SubscriberName_"__"_tsubscription.TopicName
    Set isUpdate=##class(HCC.SVR.PubSub.Subscription).IndexSubscriberTopicExists(subscriber.%Id(),topic.%Id())
    If ('isUpdate){
    Set subscription.Subscriber=subscriber
    Set subscription.Topic=topic
    Set subscription.IsSync=tsubscription.IsSync
    Set:(tsubscription.IsEnabled'="") subscription.IsEnabled=tsubscription.IsEnabled
    Set tsc=subscription.%Save()    
    If $$$ISERR(tsc) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("订阅添加失败",tsc.AsStatus(),500)
    }
    Set response.SubScription=subscription.Subscriber.SubscriberName_"__"_subscription.Topic.TopicName
    set response.ID=subscription.%Id()
    do ..%SetStatusCode(201)
    Set response.Message="订阅已添加"
    }
    Else{
        Set seSubcription=##class(HCC.SVR.PubSub.Subscription).IndexSubscriberTopicOpen(subscriber.%Id(),topic.%Id())
        Set seSubcription.IsSync=tsubscription.IsSync
        Set:(tsubscription.IsEnabled'="") seSubcription.IsEnabled=tsubscription.IsEnabled
        Set tsc=seSubcription.%Save()
        If $$$ISERR(tsc) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("订阅更新失败",tsc.AsStatus(),500)
        }
        do ..%SetStatusCode(200)
        set response.ID=seSubcription.%Id()
        Set response.Message="订阅已更新"
    }
    }
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("新增订阅错误",ex.AsStatus(),500)
    }
    Return response
}

/// 获取订阅<br/>
/// The method arguments hold values for:<br/>
///     SubscriptionID, get subscribers using ID<br/>
ClassMethod getsubscription(SubscriptionID As %String) As %DynamicObject
{
    #dim subscription as HCC.SVR.PubSub.Subscription
    #dim response as %Library.DynamicObject=##class(%Library.DynamicObject).%New()
    Try{
    Set dynamicArray =##class(%Library.DynamicArray).%New()
    If ($GET(SubscriptionID)=""){

      Set myquery="select ID from HCC_SVR_PubSub.Subscription"
      Set tStatement = ##class(%SQL.Statement).%New()
    Set qStatus = tStatement.%Prepare(myquery)
    If qStatus'=1 {Write "%Prepare failed:" Do $SYSTEM.Status.DisplayError(qStatus) Quit}
    Set rset = tStatement.%Execute()
    While rset.%Next() {
      Set ID=rset.%Get("ID")
      
      Set subscription= ##class(HCC.SVR.PubSub.Subscription).%OpenId(ID)
      Set dynamicObject =..ConvertSubscription(subscription)
	  Do dynamicArray.%Push(dynamicObject)
    }
    }
    Else{      
            Set subscription= ##class(HCC.SVR.PubSub.Subscription).%OpenId(SubscriptionID)
            Set dynamicObject =..ConvertSubscription(subscription)
            If (subscription=""){
            Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("订阅不存在",$$$OK,404)
            }       
            Do dynamicArray.%Push(dynamicObject)  
    }        
    Do ..%SetStatusCode(200)
     Set response= dynamicArray
     }
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("获取订阅错误",ex.AsStatus(),500)}
    Return response
}

ClassMethod ConvertSubscription(tsubscription As HCC.SVR.PubSub.Subscription) As %DynamicObject
{
   
    #dim subResponse as %Library.DynamicObject=##class(%Library.DynamicObject).%New()
    
    Set subResponse.SubscriptionID=tsubscription.%Id()
    Set subResponse.SubscriberName= tsubscription.Subscriber.SubscriberName
    Set subResponse.SubscriberID= tsubscription.Subscriber.%Id()
    Set subResponse.TopicName= tsubscription.Topic.TopicName
    Set subResponse.TopicID= tsubscription.Topic.%Id()
    Set subResponse.IsSync= tsubscription.IsSync
    Set subResponse.IsEnabled= tsubscription.IsEnabled
    Set subResponse.CreatedAt= tsubscription.CreatedAt
    
    Return subResponse
}

/// 删除订阅<br/>
/// The method arguments hold values for:<br/>
///     id, removesubscription<br/>
ClassMethod removesubscription(id As %Integer) As %DynamicObject
{
    try{
    Set response=##class(%Library.DynamicObject).%New()
    Set tsub=##class(HCC.SVR.PubSub.Subscription).%OpenId(id)
    If tsub=""{
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("订阅不存在",$$$OK,404)
    }
    Set subdescription=tsub.Subscriber.SubscriberName_"__"_tsub.Topic.TopicName
    Set tsc=##class(HCC.SVR.PubSub.Subscription).%DeleteId(id)
    If $$$ISERR(tsc) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("删除订阅失败",tsc.AsStatus(),500)
    }
    Set response.Message="订阅已删除"}
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("删除订阅错误",ex.AsStatus(),500)
    }
    Return response
}

/// 失败回退BO
ClassMethod RollBackConfigItem(Production As Ens.Config.Production, BusinessHostID As %String) As %Status
{
    //删除BO
  Set tsc= ##class(EnsPortal.ProductionConfig).DeleteConfigItem(Production.%Id(),BusinessHostID)

  Return tsc
}

/// 验证suscriber json字符串
ClassMethod ValidateJSON(obj As %DynamicObject, Output error As %DynamicObject) As %Status
{
   	Set error = {}
	Set error.errormessage=""
	If obj.%Get("Target") = "" {
		Set error.errormessage ="业务操作组件为空"_$CHAR(10)_error.errormessage
	}	
    If obj.%Get("SubscriberName") = "" {
		Set error.errormessage ="订阅者名为空"_$CHAR(10)_error.errormessage
	}
	If error.errormessage '= ""
    {
        Quit $$$ERROR($$$GeneralError,error.errormessage)
    }
	Quit $$$OK
}

/// 验证subscription json字符串
ClassMethod ValidateSubscriptionJSON(obj As %DynamicObject, Output error As %DynamicObject) As %Status
{
   	Set error = {}
	Set error.errormessage=""
	If obj.%Get("SubscriberName") = "" {
		Set error.errormessage ="订阅者为空"_$CHAR(10)_error.errormessage
	}	
	
	If obj.%Get("TopicName") = "" {
		Set error.errormessage ="订阅主题为空"_$CHAR(10)_error.errormessage
	}

    If obj.%GetTypeOf("IsSync")'="boolean" {
		Set error.errormessage ="请选择是否为同步发送并提供一个布尔值"_$CHAR(10)_error.errormessage
	}

      If obj.%GetTypeOf("IsEnabled")'="boolean"{
        If obj.%Get("IsEnabled")'="" {
        Set error.errormessage ="若选择是否开启则需提供一个布尔值，此值如不传则默认开启"_$CHAR(10)_error.errormessage}
	}
	If error.errormessage '= ""
    {
        Quit $$$ERROR($$$GeneralError,error.errormessage)
    }
	Quit $$$OK
}

}
